package controller.admin;
import java.io.File;
import java.util.Date;

import com.jfinal.aop.Inject;
import com.jfinal.ext.kit.DateKit;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.upload.UploadFile;

import controller.admin.base.BaseAdminController;
import model.Company;
import service.CompanyService;

public class AdminCompanyController extends BaseAdminController {
	@Inject CompanyService companyService ;

	public void list() {
	    Kv kv = getParamKv() ;
	    Integer pageNumber = getParaToInt("pageNumber", 1);
	    Integer pageSize = getParaToInt("pageSize", 10);
	    set("page", companyService.getPage( kv , pageNumber, pageSize));
	    renderJson();
	}

	public void delete() {
        Long id = getParaToLong("id");
        Ret ret = companyService.delete(id);
        renderJson(ret);
    }

	public void info() {
		Long id = getParaToLong("id");
        set("data", Company.dao.findById(id) );
        renderJson();
	}

	public void save() {
		Company company = getModel(Company.class , "x");
        Ret ret = companyService.save(company);
        renderJson(ret);
	}
	
	public void imports() {
		UploadFile file = getFile("upfile");
    	renderJson(companyService.importExcel(file.getFile()));
	}
	
	public void getUploadTpl() {
		String tplName = getPara("tplName" , "importCompanys.xls");
		String reName = getPara("reName" , "企业信息导入模板.xls");
		renderFile(tplName , reName);
	}
	
	public void export() {
		Kv kv = getParamKv() ;
		File file = companyService.exportExcel(kv);
    	renderJson("fileName" , file.getAbsolutePath() );
	}
	
	public void downloadExcel() {
		String fileName = getPara("fileName");
		File file = new File( fileName );
		String newName = "企业信息_" + DateKit.toStr(new Date()) +".xls" ;
		renderFile(file , newName);
	}
	
	public void getMarkers() {
		renderJson( companyService.getMarkers() );
	}
}
